package com.example.goods.controller;

import com.example.goods.bean.Shipping;
import com.example.goods.bean.User;
import com.example.goods.common.Const;
import com.example.goods.common.ResultResponse;
import com.example.goods.service.ShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/7/5
 */
@RequestMapping("shipping")
@RestController
public class ShippingController {
    @Autowired
    private ShippingService shippingService;

    /**
     * 添加收货地址
     * @param shipping
     * @param session
     * @return
     */
    @GetMapping("add")
    public ResultResponse add(Shipping shipping, HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ResultResponse.createByErrorMessage("用户未登陆");
        }
        return shippingService.add(user.getId(),shipping);
    }

    @GetMapping("select")
    public ResultResponse<Optional<Shipping>> select(HttpSession session, Integer id){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ResultResponse.createByErrorMessage("用户未登陆");
        }
        return shippingService.select(id);
    }

}
